package ui.listener;

import ui.view.HScrollTableView;
import ui.view.HTable;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

/*
 *  This class listens for changes made to the data in the table via the
 *  TableCellEditor. When editing is started, the value of the cell is saved
 *  When editing is stopped the new value is saved. When the oold and new
 *  values are different, then the provided Action is invoked.
 *
 *  The source of the Action is a TableCellChangeAdapter instance.
 */
public class TableClickAdapter extends MouseAdapter
{
    private HTable table;
    private TableClickAction action;

    private int row;
    private int column;
    private Object oldValue;
    private Object newValue;
    private HScrollTableView hScrollTableView;
    /**
     *  Create a TableCellChangeAdapter.
     *
     *  @param table   the table to be monitored for data changes
     *  @param action  the Action to invoke when cell data is changed
     */

    public TableClickAdapter(HScrollTableView table,TableClickAction action)
    {
        this.hScrollTableView = table;
        this.table = table.gethTable();
        this.action = action;
        this.table.addMouseListener( this );
    }

    /**
     *  Get the table of the cell that was changed
     *
     *  @return the table of the cell that was changed
     */
    public JTable getTable()
    {
        return table;
    }
//
//  Implement the PropertyChangeListener interface
//

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);

        int selectedRow = table.getSelectedRow();
//                    Map<String,String> valueMap = new HashMap<>();
        List<String> row = table.getModel().getRow(selectedRow,true);
        HTableEvent h = new HTableEvent(row);
        action.actionPerformed(e,h);
    }

}